---
sidebar_position: 51
description: How to use the OpenFeature Javascript SDK
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';



# Javascript / Typescript SDK usage

## Install dependencies

The first things we will do is install the **Open Feature SDK** and the **GO Feature Flag provider**.

<Tabs groupId="code">
  <TabItem value="yarn" label="yarn">

```shell
yarn add @openfeature/js-sdk @openfeature/go-feature-flag-provider
```

  </TabItem>
  <TabItem value="npm" label="npm">

```shell
npm i @openfeature/js-sdk @openfeature/go-feature-flag-provider
```

  </TabItem>
</Tabs>

## Initialize your Open Feature client

To evaluate the flags you need to have an Open Feature configured in you app.
This code block shows you how you can create a client that you can use in your application.

<Tabs groupId="code">
  <TabItem value="javascript" label="JavaScript">

```javascript
const {OpenFeature} = require("@openfeature/js-sdk");
const {GoFeatureFlagProvider} = require("@openfeature/go-feature-flag-provider");


// init Open Feature SDK with GO Feature Flag provider
const goFeatureFlagProvider = new GoFeatureFlagProvider({
  endpoint: 'http://localhost:1031/' // DNS of your instance of relay proxy
});
OpenFeature.setProvider(goFeatureFlagProvider);
const featureFlagClient = OpenFeature.getClient('my-app')
```

  </TabItem>

  <TabItem value="ts" label="TypeScript">

```typescript
import {EvaluationContext, OpenFeature} from "@openfeature/js-sdk";
import {GoFeatureFlagProvider} from  "@openfeature/go-feature-flag-provider";


// init Open Feature SDK with GO Feature Flag provider
const goFeatureFlagProvider: GoFeatureFlagProvider = new GoFeatureFlagProvider({
endpoint: 'http://localhost:1031/'
});
OpenFeature.setProvider(goFeatureFlagProvider);
const featureFlagClient = OpenFeature.getClient('my-app');
```

  </TabItem>
</Tabs>

## Evaluate your flag

This code block explain how you can create an `EvaluationContext` and use it to evaluate your flag.

:::note
In this example we are evaluating a `boolean` flag, but other types are available.

**Refer to the [Open Feature documentation](https://docs.openfeature.dev/docs/reference/concepts/evaluation-api#basic-evaluation) to know more about it.**
:::

<Tabs groupId="code">
  <TabItem value="javascript" label="JavaScript">

```javascript
// Context of your flag evaluation.
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
const userContext = {
  targetingKey: '1d1b9238-2591-4a47-94cf-d2bc080892f1', // user unique identifier (mandatory)
  firstname: 'john',
  lastname: 'doe',
  email: 'john.doe@gofeatureflag.org',
  admin: true, // this field is used in the targeting rule of the flag "flag-only-for-admin"
  // ...
};

const adminFlag = await featureFlagClient.getBooleanValue('flag-only-for-admin', false, userContext);
if (adminFlag) {
   // flag "flag-only-for-admin" is true for the user
} else {
  // flag "flag-only-for-admin" is false for the user
}
```

  </TabItem>

  <TabItem value="ts" label="TypeScript">

```typescript
// Context of your flag evaluation.
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
const userContext: EvaluationContext = {
  targetingKey: '1d1b9238-2591-4a47-94cf-d2bc080892f1', // user unique identifier
  firstname: 'john',
  lastname: 'doe',
  email: 'john.doe@gofeatureflag.org',
  admin: true, // this field is used in the targeting rule of the flag "flag-only-for-admin"
  // ...
};

const adminFlag = await featureFlagClient.getBooleanValue('flag-only-for-admin', false, userContext);
if (adminFlag) {
  // flag "flag-only-for-admin" is true for the user
} else {
  // flag "flag-only-for-admin" is false for the user
}
```

  </TabItem>
</Tabs>
